using UnityEngine;
using System.Collections;

public class BattleManager : MonoBehaviour {
	public static BattleManager Instance;
	
	
	public enum BSTATE {
		BATTLE_START,
		CHARACTER_START,
		CHARACTER_ACTION,
		CHARACTER_END,
		PLAYER_START,
		PLAYER_ACTION,
		ENEMY_START,
		ENEMY_ACTION,
		BATTLE_END
	};
	public BSTATE enumBattleState;
	private BSTATE enumCompState;
	
	public GameObject gPlayer;
	public GameObject gEnemy;
	
	private GameObject gFirst;
	private GameObject gNext;
	private Character csFirstChar;
	private Character csNextChar;
	private Character csPlayer;
	private Character csEnemy;
	
	private int m_nTurns = 0;
	private int m_nFirstCharTurns = 0;
	private int m_nNextCharTurns = 0;
	
	public void DealDMG(int p_nDMG, string p_strDMGSource){
		if (p_strDMGSource == csPlayer.GetName()){
			csEnemy.TakeDMG(p_nDMG);
		}
		else {
			csPlayer.TakeDMG(p_nDMG);
		}
//		if (csPlayer.GetHealth() <= 0 || csEnemy.GetHealth() <= 0)
//			print ("GameOver");
		
	}
	public void EndTurn(){
		if (csPlayer.GetTurn() == true){
			csPlayer.EndTurn();
			csEnemy.SetTurn();
		}
		else {
			csEnemy.EndTurn();
			csPlayer.SetTurn();
		}
			
	}
	void Awake () {
		csPlayer = (Character) gPlayer.GetComponent<Character>();
		csEnemy = (Character) gEnemy.GetComponent<Character>();
		
		Instance = this;	
		enumBattleState = BSTATE.BATTLE_START;
		enumCompState = BSTATE.BATTLE_END;
	}
	// Use this for initialization
	void Start () {
			
	}
	
	// Update is called once per frame
	void Update () {
		if (enumBattleState != enumCompState){
			enumCompState = enumBattleState;
			switch (enumBattleState) {
				case BSTATE.BATTLE_START:
					print("BattleStart");
					if (gPlayer.GetComponent<Character>().GetAgi() >= gEnemy.GetComponent<Character>().GetAgi()){
//						csFirstChar = (Character) gPlayer.GetComponent<Character>();
//						csNextChar = (Character) gEnemy.GetComponent<Character>();
						csPlayer.SetTurn();
						enumBattleState = BSTATE.CHARACTER_START;
						
					}
					else {
//						csFirstChar = (Character) gEnemy.GetComponent<Character>();
//						csNextChar = (Character) gPlayer.GetComponent<Character>();
						csEnemy.SetTurn();
						enumBattleState = BSTATE.CHARACTER_START;
					}
				break;
				case BSTATE.CHARACTER_START:
					
				break;
				case BSTATE.CHARACTER_ACTION:
				
				break;
				case BSTATE.CHARACTER_END:
					
				break;
				case BSTATE.PLAYER_START:
					print("PlayerStart");
				break;
				case BSTATE.PLAYER_ACTION:
					print ("PlayerAction");
				break;
				case BSTATE.ENEMY_START:
					print ("EnemyStart");
				break;
				case BSTATE.ENEMY_ACTION:
					print ("EnemyAction");
				break;
			}
				
		}
		
				
	}
	
	
	public void PlayerSkill1()
	{
		Debug.Log("SKILL 1");
	  	csPlayer.dSkill1();
	}
	
	public void PlayerSkill2()
	{
		Debug.Log("SKILL 2");
	 	csPlayer.dSkill2();
	}
	
	public void PlayerSkill3()
	{
		Debug.Log("SKILL 3");
	  	csPlayer.dSkill3();
	}
	
	public void PlayerSkill4()
	{
		Debug.Log("SKILL 4");
	  	csPlayer.dSkill4();
	}
}
